<template>
  <div class="dashboard-container">
    <!-- 数据概览卡片 -->
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card shadow="hover" class="data-card">
          <div class="card-header">
            <span class="title">总用户数</span>
            <el-icon class="icon" :style="{ color: '#409EFF' }"><User /></el-icon>
          </div>
          <div class="card-body">
            <span class="number">{{ statistics.totalUsers }}</span>
            <span class="trend">
              较上月
              <span :class="statistics.userGrowth >= 0 ? 'up' : 'down'">
                {{ Math.abs(statistics.userGrowth) }}%
                <el-icon>
                  <component :is="statistics.userGrowth >= 0 ? 'ArrowUp' : 'ArrowDown'" />
                </el-icon>
              </span>
            </span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="data-card">
          <div class="card-header">
            <span class="title">医生数量</span>
            <el-icon class="icon" :style="{ color: '#67C23A' }"><UserFilled /></el-icon>
          </div>
          <div class="card-body">
            <span class="number">{{ statistics.totalDoctors }}</span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="data-card">
          <div class="card-header">
            <span class="title">今日活跃</span>
            <el-icon class="icon" :style="{ color: '#E6A23C' }"><View /></el-icon>
          </div>
          <div class="card-body">
            <span class="number">{{ statistics.activeUsers }}</span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="data-card">
          <div class="card-header">
            <span class="title">问诊量</span>
            <el-icon class="icon" :style="{ color: '#F56C6C' }">
            </el-icon>
          </div>
          <div class="card-body">
            <span class="number">{{ statistics.consultationCount }}</span>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 图表区域 -->
    <el-row :gutter="20" class="chart-row">
      <!-- 用户增长趋势 -->
      <el-col :span="16">
        <el-card shadow="hover">
          <template #header>
            <div class="chart-header">
              <span>用户增长趋势</span>
              <el-radio-group v-model="timeRange" size="small">
                <el-radio-button label="week">本周</el-radio-button>
                <el-radio-button label="month">本月</el-radio-button>
                <el-radio-button label="year">全年</el-radio-button>
              </el-radio-group>
            </div>
          </template>
          <div class="chart-container">
            <v-chart :option="userTrendOption" autoresize />
          </div>
        </el-card>
      </el-col>

      <!-- 用户性别分布 -->
      <el-col :span="8">
        <el-card shadow="hover">
          <template #header>
            <div class="chart-header">
              <span>用户性别分布</span>
            </div>
          </template>
          <div class="chart-container">
            <v-chart :option="genderDistOption" autoresize />
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 用户角色分布 -->
    <el-row class="chart-row">
      <el-col :span="24">
        <el-card shadow="hover">
          <template #header>
            <div class="chart-header">
              <span>用户角色分布</span>
            </div>
          </template>
          <div class="chart-container">
            <v-chart :option="roleDistOption" autoresize />
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted, computed, watch } from 'vue'
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
import { LineChart, PieChart, BarChart } from 'echarts/charts'
import {
  GridComponent,
  TooltipComponent,
  LegendComponent,
  TitleComponent
} from 'echarts/components'
import VChart from 'vue-echarts'
import { User, UserFilled, View } from '@element-plus/icons-vue'
import { getDashboardData } from '@/api/dashboard'

// 注册 ECharts 必须的组件
use([
  CanvasRenderer,
  LineChart,
  PieChart,
  BarChart,
  GridComponent,
  TooltipComponent,
  LegendComponent,
  TitleComponent
])

// 统计数据
const statistics = ref({
  totalUsers: 1234,
  userGrowth: 12.5,
  totalDoctors: 56,
  activeUsers: 289,
  consultationCount: 150
})

// 时间范围选择
const timeRange = ref('month')

// 用户趋势图表配置
const userTrendOption = computed(() => ({
  tooltip: {
    trigger: 'axis'
  },
  xAxis: {
    type: 'category',
    data: statistics.value.trendDates || []
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      name: '新增用户',
      type: 'line',
      smooth: true,
      data: statistics.value.trendData || [],
      itemStyle: {
        color: '#409EFF'
      },
      areaStyle: {
        color: {
          type: 'linear',
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [
            { offset: 0, color: 'rgba(64,158,255,0.3)' },
            { offset: 1, color: 'rgba(64,158,255,0.1)' }
          ]
        }
      }
    }
  ]
}))

// 性别分布图表配置
const genderDistOption = computed(() => ({
  tooltip: {
    trigger: 'item'
  },
  legend: {
    orient: 'vertical',
    right: 10,
    top: 'center'
  },
  series: [
    {
      name: '性别分布',
      type: 'pie',
      radius: ['50%', '70%'],
      center: ['40%', '50%'],
      data: [
        { value: statistics.value.maleCount || 0, name: '男' },
        { value: statistics.value.femaleCount || 0, name: '女' }
      ],
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      }
    }
  ]
}))

// 角色分布图表配置
const roleDistOption = computed(() => ({
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'shadow'
    }
  },
  xAxis: {
    type: 'category',
    data: ['管理员', '医生', '普通用户']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      name: '用户数量',
      type: 'bar',
      data: [
        { value: statistics.value.adminCount || 0, itemStyle: { color: '#F56C6C' } },
        { value: statistics.value.doctorCount || 0, itemStyle: { color: '#67C23A' } },
        { value: statistics.value.normalUserCount || 0, itemStyle: { color: '#409EFF' } }
      ],
      barWidth: '40%'
    }
  ]
}))

// 监听时间范围变化
watch(timeRange, (newValue) => {
  fetchDashboardData()
})

// 获取仪表盘数据
const fetchDashboardData = async () => {
  try {
    const { data } = await getDashboardData(timeRange.value)
    statistics.value = data
  } catch (error) {
    console.error('获取仪表盘数据失败:', error)
  }
}

onMounted(() => {
  fetchDashboardData()
})
</script>

<style scoped>
.dashboard-container {
  padding: 20px;
}

.data-card {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;

    .title {
      font-size: 16px;
      color: #606266;
    }

    .icon {
      font-size: 24px;
    }
  }

  .card-body {
    .number {
      font-size: 24px;
      font-weight: bold;
      margin-right: 10px;
    }

    .trend {
      font-size: 14px;
      color: #909399;

      .up {
        color: #67C23A;
      }

      .down {
        color: #F56C6C;
      }
    }
  }
}

.chart-row {
  margin-top: 20px;
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chart-container {
  height: 300px;
}
</style>
